import { defineStore } from 'pinia'

interface ToastMessage {
  id: number
  type: 'success' | 'error' | 'warning'
  message: string
}

export const useToastStore = defineStore('toast', {
  state: () => ({
    messages: [] as ToastMessage[],
    counter: 0,
  }),

  actions: {
    show(type: ToastMessage['type'], message: string) {
      const id = ++this.counter
      this.messages.push({ id, type, message })

      // 3秒后自动移除
      setTimeout(() => {
        this.messages = this.messages.filter((m) => m.id !== id)
      }, 3000)
    },

    success(message: string) {
      this.show('success', message)
    },

    error(message: string) {
      this.show('error', message)
    },

    warning(message: string) {
      this.show('warning', message)
    },
  },
})
